package com.pi.pro.xcmg.am.was.web.cust.controller;




import com.pi.framework.core.controller.BaseCrudController;
import com.pi.framework.core.model.IEnabled;
import com.pi.framework.core.model.IdTreeEntity;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.ConvertUtils;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.was.fac.service.WasCustomWorkService;
import com.pi.wms.entity.TwtActTaskList;
import com.pi.wms.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.*;


@Slf4j
@Controller
@RequestMapping("/task/tasklist/ca")
public class CustApplyTasklistsController extends BaseCrudController<TwtActTaskList, Long> {


    @Autowired
    private TaskService service;

    @Autowired
    private WasCustomWorkService commonWorkService;

    @Autowired
    private TaskService taskService;


    @Override
	protected ICrudService<TwtActTaskList, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "task";
	}

	@Override
	protected TwtActTaskList newModel(HttpServletRequest request) {
		return new TwtActTaskList();
	}

	@Override
	protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
		Long userId = SecurityUtils.getLoginUserId();
		query.put("userId", userId.toString());
		super.prepareDatatableConditions(query, input);
	}

    @GetMapping({"movereq"})
    public String movereq(Model model, HttpServletRequest request,
                       RedirectAttributes redirectAttributes) {
        this.setMethod(model, "Adding");
        this.setAdding(model, true);
        model.addAttribute("params", this.getParams(request));
        model.addAttribute("m", this.newModel(request));
        return this.view(this.getPrefix() + "_move_form");
    }

    @PostMapping({"move", "move/{id}"})
    public String move(Model model, @Valid @ModelAttribute("m") TwtActTaskList m, BindingResult result, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        if (StringUtils.isNullOrBlank(m.getFromContainNumber())){
            return this.redirect("");
        }
        if (StringUtils.isNullOrBlank(m.getFromLocSeq())){
            return this.redirect("");
        }
        if (StringUtils.isNullOrBlank(m.getToLocSeq())){
            return this.redirect("");
        }
        String msg=this.commonWorkService.createHoldMoveTask(XcmgAmConstants.DIVISION_WH101_CODE, m, SecurityUtils.getLoginUsername());
        return this.redirect("");
    }



    @PostMapping("release")
    public String release(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request,
                          RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String msg="";
        if ((ids==null) || (ids.length==0)){
            addSuccessMessage(redirectAttributes, "释放数据成功");
            return this.redirect("");
        }
        // msg = this.commonWorkService.postWasTaskPlanToCtu(Arrays.asList(ids), "", SecurityUtils.getLoginUsername());
        msg = this.commonWorkService.sendWasTaskToWcs(this.taskService.findByIds(ids), "",  SecurityUtils.getLoginUsername());
        if (!StringUtils.isNullOrBlank(msg)){
            addErrorMessage(redirectAttributes, "释放失败:"+msg);
            return this.redirect("");
        }
        addSuccessMessage(redirectAttributes, "释放数据成功");
        return this.redirect("");
    }


    protected void preSave(TwtActTaskList task, HttpServletRequest request) {
        super.preSave(task, request);
    }

    protected String redirect(String relativeUrl) {
        if(!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
            relativeUrl = "/" + relativeUrl;
        }
        return "redirect:" + this.referUrl() + relativeUrl;
    }

    protected String root(String relativeUrl) {
        if(!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
            relativeUrl = "/" + relativeUrl;
        }
        return this.referUrl() + relativeUrl;
    }


    private String referUrl(){
        return "/task/tasklist/";
    }

}
